/**
 * 
 */
package com.study.netty.manyPeopleChat;

import java.util.Scanner;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

/**
 * @ClassName: NettyClient
 * @Description: 多人聊天客户端启动类
 * @author zhuqiang
 * @date 2018年12月27日 下午5:16:07
 */

// 聊天程序客户端
public class ChatClient {
	private final String host; // 服务器端IP 地址
	private final int port; // 服务器端端口号

	public ChatClient(String host, int port) {
		this.host = host;
		this.port = port;
	}

	public void run() {
		EventLoopGroup group = new NioEventLoopGroup();
		try {
			Bootstrap bootstrap = new Bootstrap().group(group).channel(NioSocketChannel.class)
					.handler(new ChannelInitializer<SocketChannel>() {
						@Override
						public void initChannel(SocketChannel ch) {
							ChannelPipeline pipeline = ch.pipeline(); // 得到Pipeline
																		// 链
							// 往Pipeline 链中添加一个解码器
							pipeline.addLast("decoder", new StringDecoder());
							// 往Pipeline 链中添加一个编码器
							pipeline.addLast("encoder", new StringEncoder());
							// 往Pipeline 链中添加一个自定义的业务处理对象
							pipeline.addLast("handler", new ChatClientHandler());
						}
					});
			Channel channel = bootstrap.connect(host, port).sync().channel();
			System.out.println("--------" + channel.localAddress().toString().substring(1) + "--------");
			Scanner scanner = new Scanner(System.in);
			while (scanner.hasNextLine()) {
				String msg = scanner.nextLine();
				channel.writeAndFlush(msg + "\r\n");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			group.shutdownGracefully();
		}
	}

	public static void main(String[] args) throws Exception {
		new ChatClient("127.0.0.1", 9999).run();
	}
}